﻿#region Copyright
// Partial Copyright (c) LogoUI Software Solutions LTD
// Author: Vladislav Spivak
// This source file is the part of LogoFX Framework http://logofx.codeplex.com
// See accompanying licences and credits.
#endregion



using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Input;
#if !WinRT
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
using LogoFX.Core;
using EventTrigger = System.Windows.Interactivity.EventTrigger;



namespace LogoFX.Infra.Interactivity
{
    /// <summary>
    /// run storyboards on hover
    /// </summary>
    public class HoverBehavior : Behavior<FrameworkElement>
    {
        #region EnterStoryboard dependency property

        public Storyboard EnterStoryboard
        {
            get { return (Storyboard)GetValue(EnterStoryboardProperty); }
            set { SetValue(EnterStoryboardProperty, value); }
        }

        public static readonly DependencyProperty EnterStoryboardProperty =
            DependencyProperty.Register("EnterStoryboard", typeof(Storyboard), typeof(HoverBehavior), new PropertyMetadata(default(Storyboard), OnEnterStoryboardChanged));

        private static void OnEnterStoryboardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoverBehavior hb = (HoverBehavior)d;
            AttachEnter(hb);
        }

        private static void AttachEnter(HoverBehavior hb)
        {
            if (hb.EnterStoryboard != null && hb.AssociatedObject != null)
                hb.EnterStoryboard.Children.Where(b => String.IsNullOrWhiteSpace(Storyboard.GetTargetName(b))).Apply(c => Storyboard.SetTarget(c, hb.AssociatedObject));
        }

        #endregion

        #region LeaveStoryboard dependency property

        public Storyboard LeaveStoryboard
        {
            get { return (Storyboard)GetValue(LeaveStoryboardProperty); }
            set { SetValue(LeaveStoryboardProperty, value); }
        }

        public static readonly DependencyProperty LeaveStoryboardProperty =
            DependencyProperty.Register("LeaveStoryboard", typeof(Storyboard), typeof(HoverBehavior), new PropertyMetadata(default(Storyboard), OnLeaveStoryboardChanged));

        private static void OnLeaveStoryboardChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            HoverBehavior hb = (HoverBehavior)d;
            AttachLeave(hb);
        }

        private static void AttachLeave(HoverBehavior hb)
        {
            if (hb.LeaveStoryboard != null && hb.AssociatedObject != null)
                hb.LeaveStoryboard.Children.Where(b => String.IsNullOrWhiteSpace(Storyboard.GetTargetName(b))).Apply(c => Storyboard.SetTarget(c, hb.AssociatedObject));
        }

        #endregion

        protected override void OnAttached()
        {
            base.OnAttached();

            AssociatedObject.MouseEnter += MouseEnter;
            AssociatedObject.MouseLeave += MouseLeave;
            AttachEnter(this);
            AttachLeave(this);
        }


        private void MouseEnter(object sender, MouseEventArgs e)
        {
            if (LeaveStoryboard != null)
                LeaveStoryboard.Stop();
            if (EnterStoryboard != null)
                EnterStoryboard.Begin();
        }

        private void MouseLeave(object sender, MouseEventArgs e)
        {
            if (EnterStoryboard != null)
                EnterStoryboard.Stop();            
            if (LeaveStoryboard != null)
                LeaveStoryboard.Begin();
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseEnter -= MouseEnter;
            AssociatedObject.MouseLeave -= MouseLeave;
            base.OnDetaching();
        }
    }
}
#endif